<template>
  <div class="home-view">
    <!-- 轮播图/横幅 -->
    <section class="hero-section">
      <div class="container">
        <div class="hero-content">
          <h1 class="hero-title">欢迎来到若依笔记博客</h1>
          <p class="hero-subtitle">分享知识，记录生活，与世界连接</p>
          <div class="hero-actions">
            <el-button type="primary" size="large" @click="$router.push('/daily')">
              浏览笔记
            </el-button>
            <el-button size="large" @click="$router.push('/register')">
              加入我们
            </el-button>
          </div>
        </div>
      </div>
    </section>

    <!-- 推荐笔记 -->
    <section class="recommended-section">
      <div class="container">
        <h2 class="section-title">推荐笔记</h2>
        <div v-loading="loading" class="notes-grid">
          <div
            v-for="note in recommendedNotes"
            :key="note.noteId"
            class="note-card"
            @click="handleNoteClick(note)"
          >
            <div class="note-header">
              <h3 class="note-title">{{ note.title || '无标题' }}</h3>
              <el-tag v-if="note.categoryName" size="small">{{ note.categoryName }}</el-tag>
            </div>
            <p class="note-summary">{{ getNoteSummary(note.content, note.summary, 100) || '暂无内容' }}</p>
            <div class="note-meta">
              <div class="note-stats">
                <span><el-icon><View /></el-icon> {{ note.viewCount || 0 }}</span>
                <span><el-icon><Star /></el-icon> {{ note.likeCount || 0 }}</span>
                <span><el-icon><Collection /></el-icon> {{ note.favoriteCount || 0 }}</span>
              </div>
              <span class="note-time">{{ formatTime(note.createTime) }}</span>
            </div>
            <div v-if="note.tags" class="note-tags">
              <el-tag
                v-for="tag in note.tags.split(',')"
                :key="tag"
                size="small"
                type="info"
                effect="plain"
              >
                {{ tag.trim() }}
              </el-tag>
            </div>
          </div>
        </div>
        
        <div v-if="!loading && recommendedNotes.length === 0" class="empty-state">
          <el-empty description="暂无推荐笔记" />
        </div>
      </div>
    </section>

    <!-- 热门标签 -->
    <section class="tags-section">
      <div class="container">
        <h2 class="section-title">热门标签</h2>
        <div class="tags-container">
          <el-tag
            v-for="tag in hotTags"
            :key="tag"
            size="large"
            effect="light"
            class="hot-tag"
            @click="handleTagClick(tag)"
          >
            {{ tag }}
          </el-tag>
        </div>
      </div>
    </section>

    <!-- 统计信息 -->
    <section class="stats-section">
      <div class="container">
        <div class="stats-grid">
          <div class="stat-item">
            <el-icon class="stat-icon"><Document /></el-icon>
            <div class="stat-content">
              <h3>1000+</h3>
              <p>精品笔记</p>
            </div>
          </div>
          <div class="stat-item">
            <el-icon class="stat-icon"><User /></el-icon>
            <div class="stat-content">
              <h3>500+</h3>
              <p>活跃用户</p>
            </div>
          </div>
          <div class="stat-item">
            <el-icon class="stat-icon"><Star /></el-icon>
            <div class="stat-content">
              <h3>10000+</h3>
              <p>点赞收藏</p>
            </div>
          </div>
          <div class="stat-item">
            <el-icon class="stat-icon"><ChatDotRound /></el-icon>
            <div class="stat-content">
              <h3>5000+</h3>
              <p>互动评论</p>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getRecommendedNotes, getHotTags } from '@/api/note'
import type { NoteDaily, NoteDocument } from '@/types/api'
import { ElMessage } from 'element-plus'
import { View, Star, Collection, Document, User, ChatDotRound } from '@element-plus/icons-vue'
import { getNoteSummary } from '@/utils/text'
import dayjs from 'dayjs'

const router = useRouter()

const loading = ref(false)
const recommendedNotes = ref<(NoteDaily | NoteDocument)[]>([])
const hotTags = ref<string[]>([])

// 获取推荐笔记
const fetchRecommendedNotes = async () => {
  try {
    loading.value = true
    const response = await getRecommendedNotes(6)
    console.log('推荐笔记响应数据:', response)
    
    if (response.code === 200 && response.data) {
      recommendedNotes.value = response.data
      console.log('设置推荐笔记数据:', recommendedNotes.value)
    } else {
      console.error('推荐笔记响应异常:', response)
      ElMessage.error(response.msg || '获取推荐笔记失败')
    }
  } catch (error) {
    console.error('获取推荐笔记失败:', error)
    ElMessage.error('获取推荐笔记失败')
  } finally {
    loading.value = false
  }
}

// 获取热门标签
const fetchHotTags = async () => {
  try {
    const response = await getHotTags()
    hotTags.value = response.data
  } catch (error) {
    console.error('获取热门标签失败:', error)
    // 使用默认标签
    hotTags.value = ['Vue3', 'Spring Boot', '前端开发', '后端开发', '数据库', '算法', '设计模式', '项目实战']
  }
}

// 处理笔记点击
const handleNoteClick = (note: NoteDaily | NoteDocument) => {
  // 根据笔记类型跳转到对应的详情页
  const type = note.hasOwnProperty('content') && note.hasOwnProperty('categoryId') ? 'daily' : 'document'
  router.push(`/note/${type}/${note.noteId}`)
}

// 处理标签点击
const handleTagClick = (tag: string) => {
  router.push({
    name: 'search',
    query: { keyword: tag }
  })
}

// 格式化时间
const formatTime = (time?: string) => {
  if (!time) return ''
  return dayjs(time).format('YYYY-MM-DD')
}

onMounted(() => {
  fetchRecommendedNotes()
  fetchHotTags()
})
</script>

<style scoped>
.home-view {
  min-height: calc(100vh - 140px);
}

/* 英雄区域 */
.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 80px 0;
  text-align: center;
}

.hero-title {
  font-size: 3rem;
  font-weight: bold;
  margin-bottom: 20px;
}

.hero-subtitle {
  font-size: 1.2rem;
  margin-bottom: 40px;
  opacity: 0.9;
}

.hero-actions {
  display: flex;
  gap: 20px;
  justify-content: center;
  flex-wrap: wrap;
}

/* 推荐笔记区域 */
.recommended-section {
  padding: 60px 0;
  background: white;
}

.section-title {
  font-size: 2rem;
  text-align: center;
  margin-bottom: 40px;
  color: #2c3e50;
}

.notes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 30px;
}

.note-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #e4e7ed;
}

.note-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.note-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.note-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
  flex: 1;
  margin-right: 10px;
}

.note-summary {
  color: #606266;
  line-height: 1.6;
  margin-bottom: 16px;
}

.note-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.note-stats {
  display: flex;
  gap: 16px;
  color: #909399;
  font-size: 14px;
}

.note-stats span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.note-time {
  color: #909399;
  font-size: 14px;
}

.note-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
}

/* 热门标签区域 */
.tags-section {
  padding: 60px 0;
  background: #f8f9fa;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
}

.hot-tag {
  cursor: pointer;
  transition: all 0.3s ease;
}

.hot-tag:hover {
  transform: scale(1.05);
}

/* 统计信息区域 */
.stats-section {
  padding: 60px 0;
  background: white;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 40px;
}

.stat-item {
  text-align: center;
  padding: 20px;
}

.stat-icon {
  font-size: 3rem;
  color: #409eff;
  margin-bottom: 16px;
}

.stat-content h3 {
  font-size: 2rem;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 8px;
}

.stat-content p {
  color: #606266;
  font-size: 1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .hero-title {
    font-size: 2rem;
  }
  
  .hero-subtitle {
    font-size: 1rem;
  }
  
  .notes-grid {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
